package captcha

import (
	"github.com/afocus/captcha"
	"image/color"
	"math/rand"
	"time"
)

func Captcha(code string) *captcha.Image {
	cap := captcha.New()
	cap.SetFont("conf/Tahoma.ttf")
	cap.SetSize(120, 60)
	cap.SetDisturbance(captcha.NORMAL)
	cap.SetFrontColor(color.Gray{3})
	cap.SetBkgColor(color.White)
	return cap.CreateCustom(code)
}

func RandomString(length int) string {
	str := "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
	var result string
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < length; i++ {
		result = result + string(str[r.Intn(len(str))])
	}
	return result
}
